using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;


using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The ClassRoomTableDB class is responsible for interacting with the database to retrieve and store information
    /// about ClassRoomTable objects.
    /// </summary>
    public class ClassRoomTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of ClassRoomTable from the underlying datasource.
        /// </summary>
        /// <param name="classRoomKey">The unique ClassRoomKey of the ClassRoomTable in the database.</param>
        /// <returns>An ClassRoomTable when the ClassRoomKey was found in the database, or null otherwise.</returns>
        public static ClassRoomTable GetItem(int classRoomKey)
        {
            ClassRoomTable myClassRoomTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocClassRoomTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@classRoomKey", classRoomKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myClassRoomTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myClassRoomTable;
            }
        }

        /// <summary>
        /// Returns a list with ClassRoomTable objects.
        /// </summary>
        /// <returns>A generics List with the ClassRoomTable objects.</returns>
        public static ClassRoomTableList GetList(int SchoolKey, int LocationKey)
        {
            ClassRoomTableList tempList = new ClassRoomTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocClassRoomTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myCommand.Parameters.AddWithValue("@locationKey", LocationKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a ClassRoomTable in the database.
        /// </summary>
        /// <param name="myClassRoomTable">The ClassRoomTable instance to save.</param>
        /// <returns>The new ClassRoomKey if the ClassRoomTable is new in the database or the existing ClassRoomKey when an item was updated.</returns>
        public static int Save(ClassRoomTable myClassRoomTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocClassRoomTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myClassRoomTable.ClassRoomKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", myClassRoomTable.ClassRoomKey);
                    }

                    if (myClassRoomTable.SchoolKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myClassRoomTable.SchoolKey);
                    }

                    if (myClassRoomTable.LocationKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@LocationKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@LocationKey", myClassRoomTable.LocationKey);
                    }

                    if (string.IsNullOrEmpty(myClassRoomTable.ClassRoomCode))
                    {
                        myCommand.Parameters.AddWithValue("@classRoomCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@classRoomCode", myClassRoomTable.ClassRoomCode);
                    }
                    if (string.IsNullOrEmpty(myClassRoomTable.ClassRoomName))
                    {
                        myCommand.Parameters.AddWithValue("@classRoomName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@classRoomName", myClassRoomTable.ClassRoomName);
                    }
                    if (string.IsNullOrEmpty(myClassRoomTable.RoomNo))
                    {
                        myCommand.Parameters.AddWithValue("@roomNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@roomNo", myClassRoomTable.RoomNo);
                    }
                    if (string.IsNullOrEmpty(myClassRoomTable.UnitNo))
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", myClassRoomTable.UnitNo);
                    }
                    if (string.IsNullOrEmpty(myClassRoomTable.LevelNo))
                    {
                        myCommand.Parameters.AddWithValue("@levelNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@levelNo", myClassRoomTable.LevelNo);
                    }
                    if (myClassRoomTable.SeatNo == null)
                    {
                        myCommand.Parameters.AddWithValue("@seatNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@seatNo", myClassRoomTable.SeatNo);
                    }
                    if (string.IsNullOrEmpty(myClassRoomTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myClassRoomTable.Remark);
                    }
                    if (myClassRoomTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myClassRoomTable.Status);
                    }
                    //if (myClassRoomTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myClassRoomTable.CreatedDateTime);
                    //}
                    //if (myClassRoomTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myClassRoomTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a ClassRoomTable from the database.
        /// </summary>
        /// <param name="classRoomKey">The ClassRoomKey of the ClassRoomTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int classRoomKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocClassRoomTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@classRoomKey", classRoomKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the ClassRoomTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static ClassRoomTable FillDataRecord(IDataRecord myDataRecord)
        {
            ClassRoomTable myClassRoomTable = new ClassRoomTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomKey")))
            {
                myClassRoomTable.ClassRoomKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myClassRoomTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
            {
                myClassRoomTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomCode")))
            {
                myClassRoomTable.ClassRoomCode = myDataRecord.GetString(myDataRecord.GetOrdinal("ClassRoomCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomName")))
            {
                myClassRoomTable.ClassRoomName = myDataRecord.GetString(myDataRecord.GetOrdinal("ClassRoomName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RoomNo")))
            {
                myClassRoomTable.RoomNo = myDataRecord.GetString(myDataRecord.GetOrdinal("RoomNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
            {
                myClassRoomTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LevelNo")))
            {
                myClassRoomTable.LevelNo = myDataRecord.GetString(myDataRecord.GetOrdinal("LevelNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SeatNo")))
            {
                myClassRoomTable.SeatNo = myDataRecord.GetInt16(myDataRecord.GetOrdinal("SeatNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myClassRoomTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myClassRoomTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myClassRoomTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myClassRoomTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myClassRoomTable;
        }
    }

}
